package com.eam.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;

import com.eam.bean.Department;
import com.eam.dao.DepartmentDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Department entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.eam.dao.Department
 * @author MyEclipse Persistence Tools
 */

public class DepartmentDAOImpl extends DepartmentDAO {
	private static final Log log = LogFactory.getLog(DepartmentDAOImpl.class);
	// property constants
	public static final String NAME = "name";
	public static final String LEADER = "leader";
	public static final String SPEC = "spec";

	protected void initDao() {
		// do nothing
	}

	public boolean save(Department department) {
		log.debug("saving Department instance");
		try {
			getHibernateTemplate().save(department);
			log.debug("save successful");
			return true;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			return false;
		}
	}

	public boolean delete(Department department) {
		log.debug("deleting Department instance");
		try {
			getHibernateTemplate().delete(department);
			log.debug("delete successful");
			return true;
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			return false;
		}
	}

	public Department updatePre(Department department) {
		log.debug("getting Department instance with id: " + department.getDepartmentId());
		try {
			Department instance = (Department) getHibernateTemplate().get(
					"com.eam.bean.Department", department.getDepartmentId());
			System.out.println("name:" + instance.getName());
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Department department) {
		log.debug("finding Department instance by example");
		try {
			List results = getHibernateTemplate().findByExample(department);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Department> findByProperty(String propertyName, Object value) {
		log.debug("finding Department instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Department as model where model."
					+ propertyName + "= ?";
			return (List<Department>)getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Department> findAll() {
		log.debug("finding all Department instances");
		try {
			String queryString = "from Department";
			return (List<Department>)getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Department merge(Department department) {
		log.debug("merging Department instance");
		try {
			Department result = (Department) getHibernateTemplate().merge(
					department);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public boolean update(Department department) {
		log.debug("attaching dirty Department instance");
		try {
			getHibernateTemplate().saveOrUpdate(department);
			log.debug("attach successful");
			return true;
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			return false;
		}
	}

	public void attachClean(Department department) {
		log.debug("attaching clean Department instance");
		try {
			getHibernateTemplate().lock(department, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DepartmentDAOImpl getFromApplicationContext(ApplicationContext ctx) {
		return (DepartmentDAOImpl) ctx.getBean("DepartmentDAO");
	}
}